<!-- 修改密码 - changePassword  -->
<template>
  <view class="">
    <!-- 标题栏 -->
    <view class="head-box ss-m-b-25">
      <view class="head-title ss-m-b-20">修改密码</view>
    </view>
    <!-- 表单项 -->
    <uni-forms
      ref="changePasswordRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
      labelWidth="140"
      labelAlign="center"
    >
		<view class="formBox">
			
		  <uni-forms-item name="oldPassword" label="旧密码">
			  <view class="contentBorder">
				<uni-easyinput
				  placeholder="请输入旧密码"
				  v-model="state.model.oldPassword"
				  type="text"
				  :inputBorder="false"
				/>
			  </view>
		  </uni-forms-item>

		  <uni-forms-item name="newPassword" label="新密码">
			  <view class="contentBorder">
				<uni-easyinput
				  type="password"
				  placeholder="请输入新密码"
				  v-model="state.model.newPassword"
				  :inputBorder="false"
				/>
			  </view>
		  </uni-forms-item>

		  <uni-forms-item name="reNewPassword" label="确认密码">
			  <view class="contentBorder">
				<uni-easyinput
				  type="password"
				  placeholder="请重新输入您的新密码"
				  v-model="state.model.reNewPassword"
				  :inputBorder="false"
				>
				  <template v-slot:right>
					<button class="ss-reset-button login-btn-start" @tap="changePasswordSubmit">
					  确认
					</button>
				  </template>
				</uni-easyinput>
			  </view>
		  </uni-forms-item>
		</view>
    </uni-forms>
	<view class="tipsBox ss-p-l-4">
		<view class="tipsBox-item">
			如密码丢失或未设置,请点击忘记密码重新设置
		</view> 
	</view>
    <view class="editPwd-btn-box ss-m-t-80">
      <button class="ss-reset-button forgot-btn" @tap="showAuthModal('resetPassword')">
        忘记密码
      </button>
    </view>
  </view>
</template>

<script setup>
  import { computed, watch, ref, reactive, unref } from 'vue';
  import sheep from '@/sheep';
  import { password } from '@/sheep/validate/form';
  import { showAuthModal, closeAuthModal } from '@/sheep/hooks/useModal';

  const changePasswordRef = ref(null);

  // 数据
  const state = reactive({
    isMobileEnd: false, // 手机号输入完毕
    model: {
      oldPassword: '', //旧密码
      newPassword: '', //新密码
      reNewPassword: '', //确认密码
    },
    rules: {
      oldPassword: password,
      newPassword: password,
      reNewPassword: {
        rules: [
          {
            required: true,
            errorMessage: '请确认密码',
          },
          {
            validateFunction: function (rule, value, data, callback) {
              if (value !== state.model.newPassword) {
                callback('两次输入的密码不一致');
              }
              if (value === state.model.oldPassword) {
                callback('新密码不能与旧密码相同');
              }
              return true;
            },
          },
        ],
      },
    },
  });

  // 6.更改密码
  async function changePasswordSubmit() {
    const validate = await unref(changePasswordRef)
      .validate()
      .catch((error) => {
        console.log('error: ', error);
      });
    if (!validate) return;
    sheep.$api.user.changePassword(state.model).then((res) => {
      if (res.error === 0) {
        sheep.$store('user').getInfo();
        closeAuthModal();
      }
    });
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';

  .contentBorder{
  	  border-bottom: 2rpx solid #D8D8D8;
  }
  .code-btn-start {
  	  border:none;
    font-family: Source Han Sans;
    font-size: 26rpx;
    font-weight: normal;
    line-height: normal;
    text-align: center;
    letter-spacing: normal;
    color: #FF6052;
  }
  .type-btn{
  	  width: 100%;
  	  margin-top: 80rpx;
  	  font-family: Source Han Sans;
  	  font-size: 26rpx;
  	  font-weight: 500;
  	  line-height: normal;
  	  text-align: center;
  	  letter-spacing: normal;
  	  color: #302F2F;
  }
  .login-btn-start{
  	  border-radius: 100rpx;
  	  background: #2B6EDE;
  	  width: 101rpx;
  	  height: 40rpx;
  	  font-family: Source Han Sans;
  	  font-size: 26rpx;
  	  font-weight: normal;
  	  line-height: 40rpx;
  	  text-align: center;
  	  letter-spacing: normal;
  	  color: #FFFFFF;
  	  margin-right: 10rpx;
  }
  .head-box{
  	  .head-title{
  		  font-family: Source Han Sans;
  		  font-size: 36rpx !important;
  		  font-weight: 500 !important;
  		  line-height: normal;
  		  text-align: center;
  		  color: #404040;
  	  }
  }
  .formBox{
  	  border-radius: 20rpx;
  	  background: #EEEEEE;
  	  padding-top: 20rpx;
  	  padding-right: 20rpx;
  }
  .tipsBox{
  		margin-top: 50rpx;
  	  .tipsBox-item{
  		  font-family: Source Han Sans;
  		  font-size: 28rpx;
  		  font-weight: normal;
  		  line-height: normal;
  		  letter-spacing: normal;
  		  color: #9E9E9E;
  	  }
  }
</style>
